package org.ssc.tickethouse.controller;

import lombok.Setter;
import org.ssc.tickethouse.entity.Audience;
import org.ssc.tickethouse.entity.Order;
import org.ssc.tickethouse.entity.Tickethouse;
import org.ssc.tickethouse.repository.AudienceRepository;
import org.ssc.tickethouse.repository.CinemaRepository;
import org.ssc.tickethouse.repository.OrderRepository;
import org.ssc.tickethouse.repository.TickethouseRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@Setter
@RequestMapping("/ticket")
public class TickethouseHandler {
    @Resource
    private AudienceRepository audienceRepository;
    @Resource
    private TickethouseRepository tickethouseRepository;
    @Resource
    private CinemaRepository cinemaRepository;
    @Resource
    private OrderRepository orderRepository;

    /**
     * Audience
     */
    @PostMapping("/audienceLogin")
    public Object audienceLogin(@RequestParam("aname") String aname,
                        @RequestParam("password") String password) {
        Object object;
        object = audienceRepository.login(aname, password);
        return object;
    }

    @PostMapping("/audienceRegister")
    public int userRegister(@RequestBody Audience audience) {
        if(audience.getAname() == null || audience.getAname().length() == 0){
            return 0;
        }
        if(audience.getAge() <16 || audience.getAge() > 150){
            return 0;
        }
        if(audience.getPassword() == null || audience.getPassword().length() == 0){
            return 0;
        }
        if(audience.getPhone() != null){
            if(!validPhone(audience.getPhone())){
                return 0;
            }
        }
        if(audience.getEmail() != null){
            if(!validEmail(audience.getEmail())){
                return 0;
            }
        }
        audience.setState(1);
        return audienceRepository.audienceRegister(audience);
    }

    @GetMapping("/findAudience/{aname}")
    public Audience findAudience(@PathVariable("aname") String aname){
        Audience audience;
        audience = audienceRepository.findAudience(aname);
        return audience;
    }

    /**
     * Cinema
     */
    @PostMapping("/cinemaLogin")
    public Object cinemaLogin(@RequestParam("cname") String cname,
                              @RequestParam("password") String password) {
        Object object;
        object = cinemaRepository.login(cname, password);
        return object;
    }

    /**
     * Tickethouse
     */
    @GetMapping("/getAllTickets")
    public List<Tickethouse> getAllTickets(){
        List<Tickethouse> tickethouseList;
        tickethouseList = tickethouseRepository.getAllTickets();
        return tickethouseList;
    }

    @PostMapping("/screen")
    public int screen(@RequestBody Tickethouse tickethouse){
        tickethouse.setState(1);
        return tickethouseRepository.insert(tickethouse);
    }

    @GetMapping("/getTicketsByMoviename/{moviename}")
    public List<Tickethouse> getTicketsByMoviename(@PathVariable("moviename") String moviename){
        List<Tickethouse> tickethouseList;
        tickethouseList = tickethouseRepository.getTicketsByMoviename(moviename);
        return tickethouseList;
    }

    @GetMapping("/buyTicket")
    public int buyTicket(@RequestParam("tid") int tid,@RequestParam("aid") int aid) {
        Tickethouse t = tickethouseRepository.getTicketByTid(tid);
        if (t != null) {
            int result1 = tickethouseRepository.updatestandby(tid);
            Order order = new Order();
            order.setTid(tid);
            order.setAid(aid);
            order.setState(1);
            int result2 = orderRepository.insert(order);
            if (result1 == 1 && result2 == 1) {
                return 1;
            } else {
                return 0;
            }
        }else {return 0;}
    }

    /**
     * Order
     */
    @GetMapping("/getOrderByAid/{aid}")
    public List<Order> getOrderByAid(@PathVariable int aid){
        List<Order> orderList;
        orderList = orderRepository.getOrderByAid(aid);
        return orderList;
    }


    public boolean validPhone(String phone){
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        return m.matches();
    }
    public boolean validEmail(String email){
        String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        return m.matches();
    }

}
